package com.gitee.wsl.mathematics.function.random

import org.kotlincrypto.random.CryptoRand
import kotlin.random.Random

//typealias SecureRandom = CryptoRand

//val SecureRandom.Companion.Default get() = CryptoRand.Default

abstract class SecureRandom:Random() {
    //fun nextBytes(buf: ByteArray): ByteArray

    //fun nextBytes(size:Int) = nextBytes(ByteArray(size))

    companion object:SecureRandom(){
        override fun nextBits(bitCount: Int): Int {
            val numBytes = (bitCount + 7) / 8
            val b = nextBytes(numBytes)

            var next = 0
            for (i in 0 until numBytes) {
                next = (next shl 8) + (b[i].toInt() and 0xFF)
            }
            return next ushr numBytes * 8 - bitCount
        }

        override fun nextBytes(array: ByteArray): ByteArray = CryptoRand.Default.nextBytes(array)
    }
}

